//
//  YCReplyViewController.m
//  YCSchool
//
//  Created by gmall on 16/5/24.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  回复

#import "YCReplyViewController.h"

@interface YCReplyViewController ()<UITextViewDelegate>
@property (nonatomic,strong) UITextView *textView;
@property (nonatomic,strong) UILabel *placeHolder;
@property (nonatomic,strong) UILabel *label;

@end

@implementation YCReplyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configView];
    
    self.navigationItem.title = @"评价回复";
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(confirmBtnClick)];
    self.navigationItem.rightBarButtonItem = item;
}

#pragma mark -- 确定
-(void)confirmBtnClick
{
    NSString *text = self.textView.text;
    if (text.length == 0) {
        [self showMsg:@"回复内容不能为空"];
        return;
    }
    
}

-(void)configView
{
    self.textView = [[UITextView alloc] initWithSize:CGSizeMake(kScreenWidth - 2 * 15, 240)];
    [self.textView setPosition:CGPointMake(15, 64+ 10) atAnchorPoint:CGPointZero];
    [self.view addSubview:self.textView];
    self.textView.delegate = self;
    [self.textView becomeFirstResponder];
    
    self.label = [[UILabel alloc] initWithSize:CGSizeMake(100, 20)];
    [self.label setPosition:CGPointMake(kScreenWidth - 15, CGRectGetMaxY(self.textView.frame) +  5) atAnchorPoint:CGPointMake(1, 0)];
    [self.view addSubview:self.label];
    self.label.font = [UIFont systemFontOfSize:13];
    self.label.text = @"200字";
    self.label.textColor = kBackgroudColor;
    self.label.textAlignment = NSTextAlignmentRight;
    [self addPlaceHolder];
    
    [self addBottomView];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
}

-(void)addBottomView
{
    UIView *bottomView = [[UIView alloc] initWithSize:CGSizeMake(kScreenWidth, kScreenHeight - CGRectGetMaxY(self.label.frame))];
    [bottomView setPosition:CGPointMake(0, CGRectGetMaxY(self.label.frame)) atAnchorPoint:CGPointZero];
    [self.view addSubview:bottomView];
    bottomView.backgroundColor = [UIColor yc_colorWithRed:251 Green:253 Blue:255];
    
    //line
    UIView *line = [[UIView alloc] initWithSize:CGSizeMake(kScreenWidth, 1)];
    [line setPosition:CGPointMake(0, 0) atAnchorPoint:CGPointZero];
    [bottomView addSubview:line];
    line.backgroundColor = kBackgroudColor;
}

-(void)addPlaceHolder
{
    self.placeHolder = [[UILabel alloc] initWithSize:CGSizeMake(100, 30)];
    [self.placeHolder setPosition:CGPointMake(5, 0) atAnchorPoint:CGPointZero];
    self.placeHolder.text = @"输入回复内容...";
    self.placeHolder.font = [UIFont systemFontOfSize:13];
    self.placeHolder.textColor = kBackgroudColor;
    [self.textView addSubview:self.placeHolder];
}


- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length > 0) {
        [self.placeHolder removeFromSuperview];
    }else{
        [self addPlaceHolder];
    }
}

-(void)showMsg:(NSString *)msg
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles: nil];
    [alertView show];
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@""]) {
        return YES;
    }
    if (textView.text.length >= 200) {
        [self showMsg:@"回复字数不能超过200字"];
        return NO;
    }
    return YES;
}


@end
